package Servicios;

import DAO.DAOCarrera;
import Dominio.Carrera;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class VisualizadorRegistroPE extends HttpServlet {
    private DAOCarrera daocarrera;

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            
            
           String respuesta = "<form name='frmRegistroPE' id='frmRegistroPE' action='GestorProgramaEducativo?action=uploadPE'enctype='multipart/form-data' method='post'><table>\n\t<tr>\n\t\t<td>Nombre del Programa:</td>\n\t\t"
                   + "<td><input type=\"text\" name=\"nombrePE\" id=\"nombrePE\" ></td>\n\t</tr>\n"
                   + "<tr><td>Carrera:</td><td><select name=\"optcarreras\" id=\"cmbCarreras\">"+this.contenidoComboCarreras()+"</select></td></tr>"
                   + "<tr><td>Ret&iacute;cula: </td><td><input type=\"file\" name=\"archivoReticula\" id=\"archivoReticula\"></td></tr>"
                   + "<tr><td colspan=\"2\" align=\"center\"><input type=\"button\" value=\"Registrar Programa\" id=\"btnRegistrarPE\">"
                   + "<input type=\"button\" id=\"btnCancelaPE\" value=\"Cancelar\"></td></tr>"
                   + "</table></form>";
           
            out.println(respuesta);
        } finally {            
            out.close();
        }
    }
    private String contenidoComboCarreras(){
        String cmbCarreras;
        String abreOption = "<option value=\"";
        String espacioValue = "\">";
        String cierraOption = "</option>\n";
        daocarrera = new DAOCarrera();
        ArrayList<Carrera> carreras = daocarrera.buscarTodasLasCarreras();
        cmbCarreras = "<option value=\"-1\">Seleccione...</option>\n";
        for(int i = 0;i<carreras.size();i++){
            Carrera carrera = carreras.get(i);
            cmbCarreras +=abreOption+carrera.getClaveCarrera()+espacioValue+carrera.getNombre()+cierraOption ;
        }
        daocarrera.cerrarConexionACarrera();
        return cmbCarreras;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
